unit oEditorArrivalManager;

interface

uses
  oProcessItem, oManager, oFrameArrivalData, oProcessObjectConstants,
  oArrivalsManager, oFormArrivalData, Sysutils, oProcessObjects,
  oGeneratorData;

const
  edArrNew = 1000;
  edArrEdit = 2000;

type
  EEditorArrivalManager = class(Exception);

  TEditorArrival = class(TManager)
    private
      aFrame : TfArrival;
      aForm: TFormArrivalData;
      aOldCount: TGeneratorData;
      aOldEvery: TGeneratorData;
      procedure clear;
      procedure show(paData : TArrivalsManager.TArrivalType;paFormType: Integer);
      procedure fillForm(paData: TArrivalsManager.TArrivalType);
    public
      constructor create(paMaster: TManager = nil);
      function ProcessObject(paItem: TProcessItem):TProcessItem; override;
  end;

implementation

uses
  oPedestriansManager, oMasterManager, oPathsManager, Dialogs, Controls;

resourcestring eEdArrManNotValidType = 'not a valid processing type';
resourcestring eEdArrManNotFound = 'arrival not found';
resourcestring eEdArrNoPedFound = 'no pedestrian found in project';
resourcestring eEdArrNoPathFound = 'no path found in project';

{ TManager }


procedure TEditorArrival.clear;
begin
  with aForm.aFrame do
    begin
      txtArrivesFrom.Text := '';
      txtArrivesTo.Text := '';
      txtName.Text := '';
      txtArriveCount.Text := '';
      txtArriveEvery.Text := '';
      cbxPedestrians.Items.Clear;
      cbxPedestrians.Text := '';
      cbxPaths.Items.Clear;
      cbxPaths.Text := '';
    end;
end;

constructor TEditorArrival.create;
begin
  aMaster := paMaster;
  aOldCount := nil;
  aOldEvery := nil;
end;

procedure TEditorArrival.fillForm(paData: TArrivalsManager.TArrivalType);
var
  pedestrian: TPedestrianManager.TPedestrianType;
  path: TPathsManager.TPathType;
begin
  if aForm = nil then exit;
  aFrame := aForm.aFrame;
  clear;
  for pedestrian in (aMaster as tMasterManager).PedestriansManager.Pedestrians do
    aFrame.cbxPedestrians.Items.Add(pedestrian.Name);
  for path in (aMaster as tMasterManager).PathsManager.Paths do
    aFrame.cbxPaths.Items.Add(path.Name);
  if paData <> nil then
    begin
      aForm.ArriveEvery := paData.NextArrivalGeneratorData;
      aForm.ArriveCount := paData.CountGeneratorData;
      aFrame.txtName.Text := paData.Name;
      aFrame.txtArrivesFrom.Text := FloatToStr(paData.ArrivesFrom);
      aFrame.txtArrivesTo.Text := FloatToStr(paData.ArrivesTo);
      aFrame.txtArriveCount.Text := paData.CountGeneratorData.toString;
      aFrame.txtArriveEvery.Text := paData.NextArrivalGeneratorData.toString;
      aFrame.cbxPedestrians.Text := paData.Pedestrian.Name;
      aFrame.cbxPaths.Text := paData.Path.Name;
      aFrame.rgpMoitureType.ItemIndex := paData.PedType;
    end;
end;

function TEditorArrival.ProcessObject(paItem: TProcessItem): TProcessItem;
var
  item: TProcessItem;
  arr: TArrivalsManager.TArrivalType;
begin

   case paItem.Code of

    PO_EDITOR_ARRIVAL_INSERT : begin
      show(nil,edArrNew);
      paItem.Free;
      result := nil;
    end;

    PO_EDITOR_ARRIVAL_EDIT : begin
      try
        if not(paItem.Data is TString) then raise EEditorArrivalManager.Create(eEdArrManNotValidType);
        item := Master.ProcessObject(TProcessItem.create(PO_ARRMAN_EXISTS,paItem.Data as TString));
        if item.Data = nil then raise EEditorArrivalManager.Create(eEdArrManNotFound);
        arr := item.Data as TArrivalsManager.TArrivalType;
        aOldCount := arr.CountGeneratorData.createCopy;
        aOldEvery := arr.NextArrivalGeneratorData.createCopy;
        show(arr,edArrEdit);
        result := nil;
      except
        on E: EEditorArrivalManager do
          begin
            //tu je kontrola, ci je prichod v pohode alebo nie
            MessageDlg(e.Message,mtInformation,[mbOK],0);
            Result := TProcessItem.create(PO_FAILURE,nil);
          end;
      end;
      paItem.Free;
    end

    else raise EEditorArrivalManager.Create('Could not process code');
   end;
end;

procedure TEditorArrival.show(paData: TArrivalsManager.TArrivalType;paFormType: Integer);
var
  mr: Integer;
  arrData: TArrivalData;
begin
  if (aMaster as TMasterManager).PedestriansManager.Pedestrians.Count < 1
    then begin
      MessageDlg(eEdArrNoPedFound,mtError,[mbOK],0);
      exit;
    end;
  if (aMaster as TMasterManager).PathsManager.Paths.Count < 1
    then begin
      MessageDlg(eEdArrNoPathFound,mtError,[mbOK],0);
      exit;
    end;
  aForm := TFormArrivalData.Create(nil);
  aForm.Master := aMaster;
  fillForm(paData);
  case paFormType of
    edArrNew: begin
      aForm.aFrame.btnOK.Visible := true;
      aForm.aFrame.btnEdit.Visible := false;
      aForm.aFrame.txtName.Enabled := true;
      aForm.ShowType := typeNew;
    end;
    edArrEdit: begin
      aForm.aFrame.btnOK.Visible := false;
      aForm.aFrame.btnEdit.Visible := true;
      aForm.aFrame.txtName.Enabled := false;
      aForm.ShowType := typeEdit;
    end;
  end;
  mr := aForm.ShowModal;
  case mr of
    mrOK: begin
      arrData := TArrivalData.create(
        aFrame.txtName.Text,aFrame.cbxPedestrians.Text,aFrame.cbxPaths.Text,
        aForm.ArriveCount,aForm.ArriveEvery,
        StrToFloat(aFrame.txtArrivesFrom.Text),StrToFloat(aFrame.txtArrivesTo.Text),
        aFrame.rgpMoitureType.ItemIndex);
      Master.ProcessObject(TProcessItem.create(PO_ARRMAN_INSERT,arrData));
      Master.ProcessObject(TProcessItem.create(PO_TREE_REFRESH_ARRIVALS_SUBLAYER, nil));
    end;
    mrCancel: begin
      if aForm.ShowType = typeNew
        then begin
          aForm.ArriveEvery.Free;
          aForm.ArriveCount.Free;
        end else begin
          paData.CountGeneratorData := aOldCount;
          paData.NextArrivalGeneratorData := aOldEvery;
        end;
    end;
    mrYes: begin
      arrData := TArrivalData.create(
        aFrame.txtName.Text,aFrame.cbxPedestrians.Text,aFrame.cbxPaths.Text,
        aForm.ArriveCount,aForm.ArriveEvery,
        StrToFloat(aFrame.txtArrivesFrom.Text),StrToFloat(aFrame.txtArrivesTo.Text),
        aFrame.rgpMoitureType.ItemIndex);
      Master.ProcessObject(TProcessItem.create(PO_ARRMAN_EDIT,arrData));
    end;
  end;
  aForm.Free;
  aForm := nil;
  aFrame := nil;
end;

end.

